package com.xhwl.centiotdata.handler.account.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xhwl.centiotdata.handler.account.AccountFactory;
import com.xhwl.centiotdata.handler.account.AccountHandler;
import com.xhwl.centiotdata.pojo.constant.HaiKangConstant;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.service.cent.IFaceSkillCentService;
import com.xhwl.centiotdata.service.impl.AccountServiceImpl;
import com.xhwl.centiotdata.suppliersystem.haikangyunmou.UserHaiKangYunMouHandler;
import com.xhwl.common.dto.cent.face.QueryFaceDTO;
import com.xhwl.common.dto.cent.pedes.UserDTO;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.common.vo.cent.face.QueryFaceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author wangyiling
 * @description 同步主数据到海康云眸
 * @date 2022/9/16 10:31
 */
@Slf4j
@Service
public class AccountHaiKangYunMouHandler extends AccountHandler{
    @Autowired
    private UserHaiKangYunMouHandler userHaiKangYunMouHandler;
    @Resource
    private IAuthAccountCentService iAuthPedestrianCentService;
    @Autowired
    private IFaceSkillCentService faceSkillCentService;

    @Autowired
    private AccountServiceImpl accountService;
    @Override
    public String syncUser(PedestrianIntegrationConfigDTO configDTO, StaffDTO staffDTO) {
        String userOpenId = "";
        UserDTO userDTO = new UserDTO();
        userDTO.setFaceUrl(staffDTO.getFaceUrl());
        userDTO.setName(staffDTO.getName());
        userDTO.setPhone(staffDTO.getPhone());
        try {
            // 新增住户人员信息
            userOpenId = userHaiKangYunMouHandler.addPerson(userDTO, configDTO,staffDTO.getAccountId());
            log.info("新增住户人员信息：{}", userOpenId);
        } catch (Exception e) {
            log.error("hik-cloud:addPerson fail：{}", e.toString());
        }
        return userOpenId;
    }

    @Override
    public void delete(AccountDTO accountDTO, Integer projectId, DeviceProjSupplierCategory integrationConfig) {
        if (null != integrationConfig) {
            // 封装集成配置
            JSONObject properties = integrationConfig.getProperties();
            Integer productId = integrationConfig.getProductId();
            PedestrianIntegrationConfigDTO config = new PedestrianIntegrationConfigDTO();
            config.setIpAndPort(properties.getString("ipAndPort"));
            config.setAk(properties.getString("ak"));
            config.setSk(properties.getString("sk"));
            config.setProductId(productId);
            config.setLoginName(properties.getString("loginName"));
            config.setPassword(properties.getString("password"));
            config.setProjectId(projectId);
            //查询关联表
            AuthAccountMapping authAccount = iAuthPedestrianCentService.query(projectId, accountDTO.getId(), integrationConfig.getProductId());
            if (null != authAccount) {
                if (!StringUtils.isEmpty(authAccount.getOpenId())){
                    log.info("personId:{}",authAccount.getOpenId());
                    userHaiKangYunMouHandler.deletePerson(authAccount.getOpenId(),config);
                }
                iAuthPedestrianCentService.deleteAccountMapping(projectId, accountDTO.getId(), authAccount.getOpenId(), integrationConfig.getProductId());
            }
        }
    }

    @Override
    public void update(StaffDTO dto, Integer projectId, DeviceProjSupplierCategory config) {
        if (null != config) {
            // 封装集成配置
            JSONObject properties = config.getProperties();
            Integer productId = config.getProductId();
            PedestrianIntegrationConfigDTO configDTO = new PedestrianIntegrationConfigDTO();
            configDTO.setIpAndPort(properties.getString("ipAndPort"));
            configDTO.setAk(properties.getString("ak"));
            configDTO.setSk(properties.getString("sk"));
            configDTO.setProductId(productId);
            configDTO.setLoginName(properties.getString("loginName"));
            configDTO.setPassword(properties.getString("password"));
            configDTO.setProjectId(projectId);

            // 从人脸库获取人脸图片地址
            List<AccountDTO> accountDTOList = accountService.queryAccount(projectId, Arrays.asList(dto.getAccountId()));
            AccountDTO accountDTO = accountDTOList.get(0);
            QueryFaceDTO queryFaceDTO = new QueryFaceDTO();
            queryFaceDTO.setAccountId(accountDTO.getId());

            UserDTO userDTO = new UserDTO();
            userDTO.setName(dto.getName());
            userDTO.setPhone(dto.getPhone());
            try {
                List<QueryFaceVO> faceVOS = faceSkillCentService.queryFace(queryFaceDTO);
                if (!CollectionUtils.isEmpty(faceVOS)) {
                    int size = faceVOS.size() - 1;
                    userDTO.setFaceUrl(faceVOS.get(size).getFaceUrl());
                }
            } catch (Exception e) {
                log.error("调用人脸失败  {}", e.getMessage());
            }

            AuthAccountMapping authAccount = iAuthPedestrianCentService.query(projectId, dto.getAccountId(), configDTO.getProductId());
            if (null != authAccount && StringUtils.isNotEmpty(authAccount.getOpenId())) {
                log.info("海康云眸更新人员：{},openId{}",userDTO,authAccount.getOpenId());
                userHaiKangYunMouHandler.updatePerson(userDTO,configDTO,authAccount.getOpenId());
            }
        }
    }

    @Override
    public void afterPropertiesSet() {
        AccountFactory.register(ProductEnum.HAIKANG_YUNMOU.getId(), this);
        log.info("海康云眸账号管理注册成功");
    }
}
